﻿//拖拽管理
function DragManager() {
    this.isDragging = false;
    this.isDropabeld = false;
    this.currentDragSource = false;
    this.currentArg = null;
    this.range = null;
    this.doDrag = function (dragSource, proxy, win, feedback) {
        var iselchange = false;
        var moveel = null;
        win = win || window.self;
        this.range = win;
        var doc = $(win.document);
        var manager = this;
        if (proxy) {
            proxy = $(proxy);
            var w = proxy.width();
            var h = proxy.height();
            proxy = proxy.clone().width(w).height(h);
            proxy = $("<div/>").append("<div class='proxyIcon' style='width:17px;height:17px;'/>").append(proxy);
            proxy.appendTo($("body", doc)).css("position", "absolute").css("zIndex", "99999").css("display", "none").addClass("proxy");
        }
        doc.bind("mousedown", null, mouseDownHandler);
        doc.bind("mousemove", null, mouseMoveHander);
        doc.bind("mouseup", null, mouseUpHander);
        //doc.bind("mouseover", null, mouseOverHander);
        doc.bind("mouseout", null, mouseOutHander);
        doc.find("body").bind("selectstart", selectstart);
        $(doc.find("body")).css("-moz-user-select", "none");
        function selectstart() {
            return false;
        }
        if (doc.length > 0) {
            var iframe = doc.find("iframe");
            iframe.each(function (i) {
                addFrameEvent(this);
            });
        }
        function addFrameEvent(frame) {
            var fdoc = frame.contentDocument ? $(frame.contentDocument) : $(frame.contentWindow.document);
            fdoc.bind("mousedown", { frameElem: frame }, mouseDownHandler);
            fdoc.bind("mousemove", { frameElem: frame }, mouseMoveHander);
            fdoc.bind("mouseup", { frameElem: frame }, mouseUpHander);
            //fdoc.bind("mouseover", { frameElem: frame }, function (evt) { alert("a"); mouseOverHander(evt) });
            fdoc.bind("mouseout", { frameElem: frame }, mouseOutHander);
            fdoc.find("body").bind("selectstart", selectstart);
            $(fdoc.find("body")).css("-moz-user-select", "none");
        }
        function removeFrameEvent(frame) {
            var fdoc = frame.contentDocument ? $(frame.contentDocument) : $(frame.contentWindow.document);
            fdoc.unbind("mousedown", mouseDownHandler);
            fdoc.unbind("mousemove", mouseMoveHander);
            fdoc.unbind("mouseup", mouseUpHander);
            //fdoc.unbind("mouseover", mouseOverHander);
            fdoc.unbind("mouseout", mouseOutHander);
            fdoc.find("body").unbind("selectstart", selectstart);
            $(fdoc.find("body")).css("-moz-user-select", "auto");
            $(fdoc.find("body")).css("cursor", "auto");
        }

        function mouseDownHandler(evt) {
            return false;
        }
        function mouseMoveHander(evt) {
            try {
                var body = $("body");
                if (evt.data && evt.data.frameElem) {
                    var frame = evt.data.frameElem;
                    var dom = frame.contentDocument ? $(frame.contentDocument) : $(frame.contentWindow.document);
                    body = dom.find("body");
                }
                //top.document.title =evt.target.nodeType //evt.target.outerHTML;
                body.css("cursor", "default");
                manager.isDragging = true;
                //if (!window.top.DragManager) {
                try {
                    window.top._DragManager = manager;
                }
                catch (ex) { }
                //}
                var x = 0, y = 0, x1 = 0, y1 = 0;
                if (manager.isDragging) {
                    manager.currentDragSource = dragSource;
                    //alert("a")
                    if (proxy) {
                        try {
                            x = x1 = (evt.pageX || evt.data.pageX) + 7;
                            y = y1 = (evt.pageY || evt.data.pageY) + 0;
                            if (evt.data && evt.data.frameElem) {
                                var winf = $(evt.data.frameElem);
                                x += winf.offset().left;
                                y += winf.offset().top;
                            }
                            else {
                            }
                            proxy.css({ "display": "block", "top": y + "px", "left": x + "px" });
                            //alert("b")
                            var icon = proxy.children(".proxyIcon");
                            icon.toggleClass("noDrop", !manager.isDropabeld);
                            icon.toggleClass("yesDrop", manager.isDropabeld);
                        }
                        catch (ex) {
                            //alert("c")
                        }
                    }

                    if (evt.target != moveel) {
                        iselchange = true;
                    }
                    if (iselchange) {
                        mouseOverHander(evt);
                        moveel = evt.target;
                    }
                }
                //清空拖动是导致的文本被选择
                if (document.selection) {
                    document.selection.empty();
                }
                else if (window.getSelection) {
                    window.getSelection().removeAllRanges()
                }
            }
            catch (ex) {
                //alert(ex.description);
            }
            return false;
        }
        function mouseUpHander(evt) {
            if (proxy) {
                proxy.remove();
            }
            if (manager.isDragging) {
                if (dragSource.dragDrop && $.isFunction(dragSource.dragDrop)) {
                    dragSource.dragDrop(dragSource, evt.target, manager, evt);
                }
            }
            manager.currentArg = null;
            manager.isDragging = false;
            window.top._DragManager = null;
            manager.isDropabeld = false;
            var body = $("body", doc);
            body.css("cursor", "auto");
            doc.unbind("mousedown", mouseDownHandler);
            doc.unbind("mousemove", mouseMoveHander);
            doc.unbind("mouseup", mouseUpHander);
            //doc.unbind("mouseover", mouseOverHander);
            doc.unbind("mouseout", mouseOutHander);
            body.unbind("selectstart", selectstart);
            body.css("-moz-user-select", "auto");
            if (doc.length > 0) {
                var iframe = doc.find("iframe");
                iframe.each(function (i) {
                    removeFrameEvent(this);
                });
            }
            manager.currentDragSource = null;
        }
        function mouseOverHander(evt) {
            var isdropable = false;
            if (manager.isDragging) {
                if (dragSource.dragEnter && $.isFunction(dragSource.dragEnter)) {
                    isdropable = dragSource.dragEnter(dragSource, evt, manager);
                    if (isdropable) {
                        if ($.isFunction($(evt.target).data("dropEnter"))) {
                            var fn = $(evt.target).data("dropEnter");
                            fn(evt);
                        }
                    }
                }
            }
            manager.isDropabeld = isdropable;
            if (!manager.isDropabeld) {
                manager.currentArg = null;
            }
        }
        function mouseOutHander(evt) {
            //DragManager.isDropabeld = false;
            if (manager.isDragging) {
                if (dragSource.dragLeave && $.isFunction(dragSource.dragLeave)) {
                    dragSource.dragLeave(dragSource, evt, manager);
                }
                if ($.isFunction($(evt.target).data("dragLeave"))) {
                    var fn = $(evt.target).data("dragLeave");
                    fn(evt);
                }
            }
        }
    }

};
DragManager.currentDragManager = function () {
    if (window.top._DragManager) {
        return window.top._DragManager;
    }
    return null;
};


$.fn.ds_Dragable = function (data, proxy, format, dragEnter, dragLeave, dragDrop, offset, win) {
    var isDown;
    var startPoint = null;
    var obj = this;
    offset = offset || 5;
    var dragObj = null;
    var dragManager = null;
    var dragData = null;
    function mouseUp(evt) {
        isDown = false;
        startPoint = null;
        $(document).unbind("mouseup", mouseUp);
        $("body").css("-moz-user-select", "auto");
    }
    function moveDown(evt) {
        dragObj = evt.target;
        isDown = true;
        startPoint = { x: evt.pageX, y: evt.pageY };
        $(document).unbind("mouseup", mouseUp);
        $(document).bind("mouseup", mouseUp);
        obj.bind("mousemove", moveMove);
        $("body").bind("selectstart", function () { return false; });
        $("body").css("-moz-user-select", "none");
        if ($.isFunction(data)) {
            dragData = data();
        }
        else {
            dragData = data;
        }
        return false;
    }
    function moveMove(evt) {
        evt.preventDefault();
        if (isDown) {
            if (Math.abs(evt.pageX - startPoint.x) > offset || Math.abs(evt.pageY - startPoint.y) > offset) {
                var ds = new DragSource(dragData, format, dragEnter, dragLeave, dragDrop);
                var pclone = null
                if (proxy && proxy.length > 0) {
                    var pclone = proxy.clone();
                    var html = pclone.html();
                    html = html.replace("[text]", dragObj.innerHTML);
                    pclone.html(html);
                }
                win = win || window.self;
                if (!dragManager) {
                    dragManager = new DragManager();
                }
                dragManager.doDrag(ds, pclone, win);
                obj.unbind("mousemove", moveMove);
                $(document).unbind("mouseup", mouseUp);
                mouseUp();
            }
        }
    }
    obj.bind("mousedown", moveDown);
};

$.fn.ds_DragDrop = function (data, proxy, format, callback, offset, win) {
    var obj = this;

    var isDown;
    var startPoint = null;
    offset = offset || 5;
    var dragObj = null;
    var dragManager = null;
    var dragData = null;
    var proxyObj = null;
    function cancelDocumentSelect() {
        return false;
    }
    function mouseUp(evt) {
        isDown = false;
        startPoint = null;
        $(document).unbind("mouseup", mouseUp);
        $("body").css("-moz-user-select", "auto");
        $("body").unbind("selectstart", cancelDocumentSelect);
    }
    function moveDown(evt) {
        dragObj = evt.target;
        isDown = true;
        startPoint = { x: evt.pageX, y: evt.pageY };
        $(document).unbind("mouseup", mouseUp);
        $(document).bind("mouseup", mouseUp);
        obj.bind("mousemove", moveMove);
        $("body").bind("selectstart", cancelDocumentSelect);
        $("body").css("-moz-user-select", "none");
        if ($.isFunction(data)) {
            dragData = data();
        }
        else {
            dragData = data;
        }

        return false;
        //evt.stopPropagation();
    }
    function moveMove(evt) {
        evt.preventDefault();
        if (isDown) {
            if (Math.abs(evt.pageX - startPoint.x) > offset || Math.abs(evt.pageY - startPoint.y) > offset) {
                obj.unbind("mousemove", moveMove);
                $(document).unbind("mouseup", mouseUp);
                if ($.isFunction(callback.dragBefore)) {
                    if (callback.dragBefore(format) == false) {
                        return;
                    }
                }
                var dragEnter = callback.dragEnter;
                var dragMove = callback.dragMove;
                var dragLeave = callback.dragLeave;
                var dragDrop = callback.dragDrop;
                var d = null;
                if (!dragData) {
                    d = $(evt.currentTarget);
                    dragData = d;
                }
                else {
                    d = dragData;
                }
                var ds = new DragSource(d, format, dragEnter, dragLeave, dragDrop);
                var pclone = null
                if (!proxyObj) {
                    if ($.isFunction(proxy)) {
                        proxyObj = proxy(evt.target);
                    }
                    else {
                        proxyObj = proxy;
                    }
                }
                if (proxyObj && proxyObj.length > 0) {
                    var pclone = proxyObj.clone();
                    var html = pclone.html();
                    html = html.replace("[text]", dragObj.innerHTML);
                    pclone.html(html);
                }
                win = win || window.self;
                if (!dragManager) {
                    dragManager = new DragManager();
                }
                dragManager.doDrag(ds, pclone, win);
                mouseUp();
            }
        }
        //return false;
    }
    if (obj.data("dropdragbind")) {
        return;
    }
    else {
        obj.bind("mousedown", moveDown);
        obj.each(function (i, el) {
            $.data(el, "dropdragbind", true);
        });
    }

};

//拖动源
function DragSource(target, format, dragEnter, dragLeave, dragDrop) {
    this.target = target;
    this.format = format;
    this.dragEnter = dragEnter;
    this.dragLeave = dragLeave;
    this.dragDrop = dragDrop;
    //this.dragMove = dragMove;
    var dataList = [];
}

//拖拽结束===================
